package ml.kgc.dao;

import java.sql.*;

public class News1 {
    //查询新闻id和标题
    public static void main(String[] args) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //加载不同数据库厂商提供的驱动（需要导入jar包mysql-connector-java-5.1.0-bin）
            Class.forName("com.mysql.cj.jdbc.Driver");
            //（1）.铺路(获取连接Connection)
            String url = "jdbc:mysql://localhost:3306/kgcnews";
            conn = DriverManager.getConnection(url,"root","root");
            //（2）下圣旨（sql命令）
            String sql = "SELECT id,title FROM news_detail";
            //（3）找一个小太监执行升值(statement/PreparedStatement)
             stmt = conn.createStatement();
            //（4）拉回西瓜（返回结果集ResultSet）
            rs = stmt.executeQuery(sql);
            while (rs.next()){
                int id = rs.getInt("id");
                String title = rs.getString("title");
                System.out.println(id+"---"+title);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //（5）关闭城门（释放资源）
            try {
                rs.close();
                stmt.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }





    }
}
